package net.wy.commons.util;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-8-16
 * Time: 上午9:34
 * 显示分页工具
 */
public class PageDisplayUtil {
    public static void displayPage(HttpServletRequest request,int page,int displayNum,int pageCount){
        int pagenumber = displayNum;
        int pagecenter = pagenumber / 2 - 1;
        int pagebet = pagenumber / 2 + 1;
        int beginPage = 1;
        int endPage = 1;

        if (page < pagebet) {
            beginPage = 1;
        } else {
            beginPage = page - pagecenter;
        }
        int totalPages = pageCount;
        if (page + pagecenter > totalPages) {
            endPage = totalPages;
        } else {
            endPage = page + pagecenter;
        }

        if (page + pagecenter < pagenumber) {
            endPage = pagenumber;
        }

        if (endPage - page < pagecenter) {
            beginPage = totalPages - (pagenumber - 1);
            if (beginPage != 1) {
                beginPage += 1;
            }
        }

        if (beginPage <= 0) {
            beginPage = 1;
        }

        if (endPage > totalPages) {
            endPage = totalPages;
        }
        request.setAttribute("beginPage", beginPage);
        request.setAttribute("pagebet", pagebet);
        request.setAttribute("endPage", endPage);
    }
}
